package com.wing.productionPlan.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.productionPlan.dao.QualityTestingRecordDetailDao;
import com.wing.productionPlan.model.entity.ProductionProcessDevice;
import com.wing.productionPlan.model.entity.QualityTestingRecordDetail;
import com.wing.productionPlan.model.request.QualityTestingRecordDetailForm;
import com.wing.productionPlan.model.response.ProductionPlanVO;
import com.wing.productionPlan.model.response.QualityTestingRecordDetailVO;
import com.wing.qualityTesting.model.response.QualityDetailRecordInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 质检记录详情表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class QualityTestingRecordDetailServiceImpl extends ServiceImpl<QualityTestingRecordDetailDao, QualityTestingRecordDetail> implements QualityTestingRecordDetailService {

    @Autowired
    private ProductionProcessDeviceService productionProcessDeviceService;
    @Autowired
    private ProductionPlanService productionPlanService;

    @Override
    public int add(QualityTestingRecordDetailForm qualityTestingRecordDetailForm) {

        //校验

        QualityTestingRecordDetail qualityTestingRecordDetail =new QualityTestingRecordDetail();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(QualityTestingRecordDetail.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(qualityTestingRecordDetailForm, qualityTestingRecordDetail, copyOptions);

        return baseMapper.insert(qualityTestingRecordDetail);
    }

    @Override
    public List<QualityTestingRecordDetailVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<QualityTestingRecordDetailVO> pageList4app(Page page, Map<String, Object> params) {
        List<QualityTestingRecordDetailVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public QualityTestingRecordDetailVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public Map<String,Object> getProcessOutputAndYield(Long processId, Long productionPlanId) {
        //查询工序名称

        Map<String,Object> map = new HashMap<>();
        //当前计划下工序的已排产数量
//        Integer productionQuantity = productionProcessDeviceService.getSumProductionQuantity(processId, productionPlanId);
//        map.put("productionQuantity",productionQuantity);
//        //计划剩余排产数量
//        ProductionPlanVO planVO = productionPlanService.get4appById(productionPlanId);
//        if(planVO != null){
//            map.put("planQuantity",planVO.getProductionQuantity() - productionQuantity);
//        }
        //工序平均合格率
        Map<Integer, Object> processMap = baseMapper.getProcessAmountAndQualifiedQuantity(processId);
        String amount = processMap.get("amount").toString();
        String qualifiedQuantity = processMap.get("qualifiedQuantity").toString();
        if(!qualifiedQuantity.equals("0")){
            BigDecimal bdAmount = new BigDecimal(amount);
            BigDecimal bdQualifiedQuantity = new BigDecimal(qualifiedQuantity);
            BigDecimal yield = bdQualifiedQuantity.divide(bdAmount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
            map.put("yield",yield);
        }else{
            map.put("yield",0);
        }
        return map;
    }

    @Override
    public List<QualityTestingRecordDetailVO> getQualityTestingRecordDetail(Long qualityTestingRecordId) {
        return baseMapper.getQualityTestingRecordDetail(qualityTestingRecordId);
    }

    /**
     *
     * @param taskId
     * @return
     */
    @Override
    public QualityDetailRecordInfoVo getDetailRecordValue(Long taskId,Long recordDetailId) {
        return baseMapper.getDetailRecordValue(taskId,recordDetailId);
    }
}
